<template>
  <div class="app-container">
    <!-- 
      搜索条件
     -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label-width="120px" label="礼物名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入礼物名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label-width="120px" label="平台价格/金币" prop="platformPrice">
        <el-input
          v-model="queryParams.platformPrice"
          placeholder="请输入平台价格/金币"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label-width="120px" label="真实价格/元" prop="realPrice">
        <el-input
          v-model="queryParams.realPrice"
          placeholder="请输入真实价格/元"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label-width="120px" label="分类" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择分类" clearable size="small">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label-width="120px" label="标签" prop="lable">
        <el-input
          v-model="queryParams.lable"
          placeholder="请输入标签"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label-width="120px" label="是否上架" prop="isListing">
        <el-select v-model="queryParams.isListing" placeholder="请选择是否上架" clearable size="small">
          <el-option
            v-for="dict in isListingOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label-width="120px" label="是否支持连击" prop="isDoubleClick">
        <el-select v-model="queryParams.isDoubleClick" placeholder="请选择是否支持连击" clearable size="small">
          <el-option
            v-for="dict in isDoubleClickOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 
      操作按钮
     -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['finance:gift:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['finance:gift:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['finance:gift:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['finance:gift:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>


    <!-- 
      列表数据
     -->
    <el-table v-loading="loading" :data="giftList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="礼物名称" align="center" prop="name" />
      <el-table-column label="平台价格/金币"  width="120" align="center" prop="platformPrice" />
      <el-table-column label="真实价格/元"  width="120" align="center" prop="realPrice" />
      <el-table-column label="图标" width="100" align="center" prop="icon" >
        <template slot-scope="scope">
          <el-avatar shape="square" :fit="fit" :src="scope.row.icon"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column label="排序"  width="70"  align="center" prop="porder" />
      <el-table-column label="分类"  width="120"  align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="typeOptions" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="标签"  width="120"  align="center" prop="lable" />
      <el-table-column label="是否上架" align="center" prop="isListing">
        <template slot-scope="scope">
          <dict-tag :options="isListingOptions" :value="scope.row.isListing"/>
        </template>
      </el-table-column>
      <el-table-column label="上架时间" align="center" prop="upListingDate" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.upListingDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="下架时间" align="center" prop="underListingDate" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.underListingDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否支持连击"  width="120"  align="center" prop="isDoubleClick">
        <template slot-scope="scope">
          <dict-tag :options="isDoubleClickOptions" :value="scope.row.isDoubleClick"/>
        </template>
      </el-table-column>
      <!-- <el-table-column label="是否有赠送动画"  width="120"  align="center" prop="isAnimation">
        <template slot-scope="scope">
          <dict-tag :options="isAnimationOptions" :value="scope.row.isAnimation"/>
        </template>
      </el-table-column>
      <el-table-column label="赠送动画地址"  width="120"  align="center" prop="animationUrl" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['finance:gift:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['finance:gift:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改打赏礼物对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="45%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="礼物名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入礼物名称" style="width: 90%"/>
        </el-form-item>
        <el-form-item label="平台价格/金币" prop="platformPrice">
          <el-input v-model="form.platformPrice" placeholder="请输入平台价格/金币" type="number" style="width: 90%"/>
        </el-form-item>
        <el-form-item label="真实价格/元" prop="realPrice">
          <el-input v-model="form.realPrice" placeholder="请输入真实价格/元" type="number" style="width: 90%"/>
        </el-form-item>
        <el-form-item label="排序" prop="porder">
          <el-input v-model="form.porder" placeholder="请输入排序" type="number" style="width: 90%"/>
        </el-form-item>
        <el-form-item label="标签" prop="lable">
          <el-input v-model="form.lable" placeholder="请输入标签" style="width: 90%"/>
        </el-form-item>
        <el-form-item label="图标">
          <ImageUploadQN v-model="form.icon"/>
        </el-form-item>
        <el-form-item label="分类" prop="type">
          <el-select v-model="form.type" placeholder="请选择分类">
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否上架" prop="isListing">
          <el-select v-model="form.isListing" placeholder="请选择是否上架">
            <el-option
              v-for="dict in isListingOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否支持连击" prop="isDoubleClick">
          <el-select v-model="form.isDoubleClick" placeholder="请选择是否支持连击">
            <el-option
              v-for="dict in isDoubleClickOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="是否有赠送动画" prop="isAnimation">
          <el-select v-model="form.isAnimation" placeholder="请选择是否有赠送动画">
            <el-option
              v-for="dict in isAnimationOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="赠送动画地址" prop="animationUrl">
          <el-input v-model="form.animationUrl" placeholder="请输入赠送动画地址" style="width: 90%"/>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGift, getGift, delGift, addGift, updateGift, exportGift } from "@/api/finance/gift";

export default {
  name: "Gift",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 打赏礼物表格数据
      giftList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 分类字典
      typeOptions: [],
      // 是否上架字典
      isListingOptions: [],
      // 是否支持连击字典
      isDoubleClickOptions: [],
      // 是否有赠送动画字典
      isAnimationOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        platformPrice: null,
        realPrice: null,
        type: null,
        lable: null,
        isListing: null,
        isDoubleClick: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "礼物名称不能为空", trigger: "blur" }
        ],
        platformPrice: [
          { required: true, message: "平台价格/金币不能为空", trigger: "blur" }
        ],
        realPrice: [
          { required: true, message: "真实价格/元不能为空", trigger: "blur" }
        ],
        icon: [
          { required: true, message: "图标不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "分类不能为空", trigger: "change" }
        ],
        isListing: [
          { required: true, message: "是否上架不能为空", trigger: "change" }
        ],
        isDoubleClick: [
          { required: true, message: "是否支持连击不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sz_gift").then(response => {
      this.typeOptions = response.data;
    });
    this.getDicts("DMC001").then(response => {
      this.isListingOptions = response.data;
    });
    this.getDicts("DMC001").then(response => {
      this.isDoubleClickOptions = response.data;
    });
    this.getDicts("DMC001").then(response => {
      this.isAnimationOptions = response.data;
    });
  },
  methods: {
    /** 查询打赏礼物列表 */
    getList() {
      this.loading = true;
      listGift(this.queryParams).then(response => {
        this.giftList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        platformPrice: null,
        realPrice: null,
        icon: null,
        type: null,
        lable: null,
        isListing: null,
        isDoubleClick: null,
        isAnimation: null,
        animationUrl: null,
        porder: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加打赏礼物";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGift(id).then(response => {
        this.form = response.data;
        this.form.upListingDate = null;
        this.form.underListingDate = null;
        this.open = true;
        this.title = "修改打赏礼物";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGift(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGift(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除打赏礼物编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delGift(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有打赏礼物数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportGift(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
